<template>
  <vol-box
    :lazy="true"
    v-model="model"
    title="合同商品库存"
    :height="400"
    :width="1200"
    :padding="5"
    :onModelClose="onModelClose"
  >
    <div>
      <salesContractInventory
        ref="salesContractInventory"
      ></salesContractInventory>
    </div>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="onSelect"
          >确认</el-button
        >
      </div>
    </template>
  </vol-box>
</template>
  <script>
import VolBox from "@/components/basic/VolBox.vue";
import salesContractInventory from "@/views/salesinfo/sales_contract_inventory/q_sales_contract_inventory.vue";
//这里使用的vue2语法，也可以写成vue3语法
export default {
  components: {
    "vol-box": VolBox,
    salesContractInventory,
  },
  data() {
    return {
      model: false,
    };
  },
  methods: {
    async onModelClose() {
      this.$refs.salesContractInventory.searchFormFields.合同 = null;
      this.$refs.salesContractInventory.searchFormFields.未销数量 = [
        null,
        null,
      ];
      this.$refs.salesContractInventory.searchFormFields.合同价格 = [
        null,
        null,
      ];
      this.$refs.salesContractInventory.searchFormFields.库位 = null;
      this.$refs.salesContractInventory.searchFormFields.编码 = null;
      this.$refs.salesContractInventory.searchFormFields.名称 = null;
      this.$refs.salesContractInventory.searchFormFields.可用数量 = [
        null,
        null,
      ];
      this.$refs.salesContractInventory.searchFormFields.入库时间 = [
        null,
        null,
      ];
      // await this.$refs.salesContractInventory.$refs.grid.search();
    },
    open(Voucher_sub) {
      this.model = true;
      let setIntervalTiem = setInterval(() => {
        if (this.$refs.salesContractInventory) {
          clearInterval(setIntervalTiem);
          this.$refs.salesContractInventory.searchFormFields.合同 = Voucher_sub;
          this.$refs.salesContractInventory.$refs.grid.search();
        }
      });
    },

    onSelect() {
      let rowObj = this.$refs.salesContractInventory.$refs.grid.getSelectRows();
      if (!rowObj.length) {
        this.base.ElMessageFn("请选择一条数据");
        return (this.model = true);
      }
      // let AuditStatusSome = rowObj.some((item) => item.AuditStatus != 1);
      // if (AuditStatusSome) {
      //   this.$message.error("请勿选择审核未通过单据");
      //   return;
      // }

      this.$emit("parentCall", ($parent) => {
        let detailDate = $parent.$refs.detail.rowData;
        let arrID = [];
        
        detailDate.forEach((item) => {
          let index = rowObj.findIndex((x) => {
            return x.库存ID == item.Inventory_ID ? arrID.push({ID:x.库存ID,goods_name:x.名称}) : "";
          });
          if (index != -1) {
            rowObj.splice(index, 1);
          }
        });
        let strTitle = "";
        arrID.forEach(item=>{
          if(strTitle!=""){
            strTitle+=","+item.goods_name
          }else{
            strTitle=item.goods_name
          }
        })
        try {
          if (strTitle != "") {
            this.base.ElMessageFn("商品" + strTitle + "已存在销售单明细");
          }
          let arr = rowObj.map((item) => {
            return {
              Goods_sub: item.编码,
              Goods_name: item.名称,
              Goods_num: item.未销数量,
              Total_price: item.合同金额,
              Tax_exclusive_contract_total_price:item.无税总价,
              WHSE_name: item.库位,
              AvailableQuantity: item.可用数量,
              sales_contractList_ID: item.ID,
              Sales_contract_voucher_sub: item.合同,
              Inventory_ID: item.库存ID,
            };
          });
          $parent.$refs.detail.rowData.unshift(...arr);
        } catch (error) {
          console.error(error);
        }
      });
      this.onModelClose();
      return (this.model = false);
    },
  },
};
</script>